package com.ideaaedi.springcloud.jd.commonspring.config;

import com.ideaaedi.commonspring.validation.LogLocalValidatorFactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * 增强LocalValidatorFactoryBean， 若jsr303校验失败，则记录被校验对象的内容
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Configuration
public class SpringValidatorAdapterConfig {
    
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public static LocalValidatorFactoryBean defaultValidator(ApplicationContext applicationContext) {
        LogLocalValidatorFactoryBean factoryBean = new LogLocalValidatorFactoryBean();
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(applicationContext);
        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
        return factoryBean;
    }
    
}